package com.atguigu.dga.governance.assessor;

import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

public abstract class Assessor {

    public final GovernanceAssessDetail assess(AssessParam assessParam){
        // 1 填默认值 日期 表名  指标名  时间  默认给10分  每个考评器也一样
        GovernanceAssessDetail governanceAssessDetail = new GovernanceAssessDetail();
        governanceAssessDetail.setAssessDate(assessParam.getAssessDate());
        governanceAssessDetail.setTableName(assessParam.getTableMetaInfo().getTableName());
        governanceAssessDetail.setSchemaName(assessParam.getTableMetaInfo().getSchemaName());
        governanceAssessDetail.setMetricId(assessParam.getGovernanceMetric().getId().toString());
        governanceAssessDetail.setMetricName(assessParam.getGovernanceMetric().getMetricName());
        governanceAssessDetail.setGovernanceType(assessParam.getGovernanceMetric().getGovernanceType());

        String tecOwnerUserName = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getTecOwnerUserName();
        governanceAssessDetail.setTecOwner(tecOwnerUserName==null?"未填写":tecOwnerUserName);
        governanceAssessDetail.setAssessScore(BigDecimal.TEN);
        governanceAssessDetail.setCreateTime(new Date());

        // 2 核心考评逻辑 打分 找到问题 每个考评器不一样
        try {
            assessProblem(governanceAssessDetail ,assessParam);
            return governanceAssessDetail;
        }catch (Exception e){  // 3 处理异常  每个考评器也一样
            governanceAssessDetail.setIsAssessException("1");
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter=new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            int maxLen= Math.min(stringWriter.toString().length(),2000  );
            governanceAssessDetail.setAssessExceptionMsg(stringWriter.toString().substring(0,maxLen));
            return  governanceAssessDetail;
        }


    }

    protected abstract   void   assessProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception;

}
